package com.qunchuang.timers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qunchuang.config.LiftIpConfig;
import com.qunchuang.constant.LiftAgreementConstant;
import com.qunchuang.constant.LiftConstant;
import com.qunchuang.constant.LiftStorageLocationConstant;
import com.qunchuang.constant.LiftTaskStateConstant;
import com.qunchuang.mapper.*;
import com.qunchuang.pojo.dto.AgvLiftEndFloorDto;
import com.qunchuang.pojo.dto.AgvLiftStorageLocationDto;
import com.qunchuang.pojo.entity.*;
import com.qunchuang.pojo.vo.LiftFirefightingVo;
import com.qunchuang.pojo.vo.ResponseVo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelId;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

import static com.qunchuang.netty.Client.CHANNEL;
import static com.qunchuang.netty.Handler.LIFT_SERVE_STATE_MAP;

@Slf4j
@Component
public class LiftTaskStateTimersTcp {
    @Autowired
    private LiftAgvcTaskMapper liftAgvcTaskMapper;
    @Autowired
    private LiftMapper liftMapper;
    @Autowired
    private LiftStorageLocationMapper liftStorageLocationMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LiftAgvcHistoryMapper liftAgvcHistoryMapper;
    @Autowired
    private PointsMapper pointsMapper;
    @Autowired
    private LiftIpConfig liftIpConfig;
    @Autowired
    private liftAgreementMapper liftAgreementMapper;

    public static final HashMap<String, String> LIFT_STATE_MAP = new HashMap<>();

    public static final HashMap<String, String> SEND_DIRECTIVES_MAP = new HashMap<>();

    @Transactional(rollbackFor = Exception.class)
    public void stateTimers() throws Exception {
        //消防模式开启查询出所有电梯
//        QueryWrapper<PointsEntity> pointsEntityQueryWrapper = new QueryWrapper<>();
//        pointsEntityQueryWrapper.eq("lift_healthy_state", LiftPointsConstant.LIFT_REQUEST_POINT_AVAILABLE);
//        List<PointsEntity> pointsEntities = pointsMapper.selectList(pointsEntityQueryWrapper);
//        for (PointsEntity pointsEntity : pointsEntities) {
//            String liftId = pointsEntity.getLiftId();
//            //todo 检查电梯消防模式是否开启
//            for (ChannelFuture channelFuture : CHANNEL.keySet()) {
//                log.info("检查消防模式是否开启");
//                String liftIdString = CHANNEL.get(channelFuture);
//                if (Objects.equals(liftIdString, liftId)) {
//                    Channel channel = channelFuture.channel();
//                    String msg = "aa0900014000000000bb";
//                    ByteBuf buf = Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8);
//                    channel.writeAndFlush(buf);
//                    break;
//                }
//            }
//        }
        ArrayList<LiftAgvcTaskEntity> liftAgvcTaskList = liftAgvcTaskMapper.selectAllTask();
        if (liftAgvcTaskList != null && !liftAgvcTaskList.isEmpty()) {
            //查询任务表中有数据
            for (LiftAgvcTaskEntity liftAgvcTask : liftAgvcTaskList) {
                String liftId = liftAgvcTask.getLiftId();
                Integer liftEndFloor = liftAgvcTask.getLiftEndFloor();
                Integer liftStartFloor = liftAgvcTask.getLiftStartFloor();
                String storageLocationId = liftAgvcTask.getStorageLocationId();
                String agvId = liftAgvcTask.getAgvId();
                Integer id = liftAgvcTask.getId();
                Integer booking = liftAgvcTask.getBooking();
                String requestDot = liftAgvcTask.getRequestDot();
                Date startTime = liftAgvcTask.getStartTime();
                //根据电梯查询电梯ip，端口号
                QueryWrapper<LiftEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("lift_id", liftId);
                LiftEntity getLiftEntity = liftMapper.selectOne(queryWrapper);
                String ip = getLiftEntity.getLiftIp();
                Integer port = getLiftEntity.getLiftPort();
                AgvLiftStorageLocationDto dto = new AgvLiftStorageLocationDto();
                dto.setAgvId(agvId);
                dto.setLiftCode(liftId);
                dto.setStoragePoint(storageLocationId);
                AgvLiftEndFloorDto agvLiftEndFloorDto = new AgvLiftEndFloorDto();
                agvLiftEndFloorDto.setAgvId(agvId);
                agvLiftEndFloorDto.setLiftCode(liftId);
                agvLiftEndFloorDto.setArriveFloor(liftEndFloor);

                ArrayList<LiftFirefightingVo> liftFirefightingList = new ArrayList<>();
                LiftFirefightingVo firefightingDto = new LiftFirefightingVo();
                switch (liftAgvcTask.getState()) {
                    case 0:
                        log.info("执行状态0，当前agv：{}", agvId);
//                        for (String liftId : CHANNEL.keySet()) {
//                            ChannelFuture channelFuture = CHANNEL.get(liftId);
//                            ChannelId channelId = channelFuture.channel().id();
                        //TODO 电梯状态为0，分配了电梯储位，进行呼梯
                        //先进行读取电梯信息，是否为空闲状态
                        ChannelFuture channelFuture = CHANNEL.get(liftId);
                        if (channelFuture != null) {
                            Channel channel = channelFuture.channel();
                            int size = LIFT_SERVE_STATE_MAP.size();
                            if (size == 0) {
                                //查询电梯协议映射表
                                Integer commandWords = selectLiftAgreement(LiftAgreementConstant.SELECT_LIFT_STATE);
                                //查询电梯当前状态
                                ByteBuf byteBuf1 = selectLiftMsg(commandWords);
//                        ByteBuf buf = Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8);
                                String s = ByteBufUtil.hexDump(byteBuf1);
                                log.info("查询电梯命令：{}", s);
                                ChannelFuture future  = channel.writeAndFlush(byteBuf1);
                                if (future.isSuccess()){
                                    log.info("消息发送成功");
                                }
                                log.info("map中的参数为：{}", LIFT_SERVE_STATE_MAP.keySet());
                                log.info("size长度{}", size);
                            }
                            if (size != 0) {
                                String getLiftStateLiftID = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE);
                                if (getLiftStateLiftID != null) {
                                    String subLiftId = getLiftStateLiftID.substring(1);
                                    String subLiftState = getLiftStateLiftID.substring(0, 1);
                                    log.info("电梯工作获取map{}", getLiftStateLiftID);
                                    if ("1".equals(subLiftState) && subLiftId.equals(liftId)) {
                                        log.info("查询电梯map是否还有参数1：{}", LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE));
                                        QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
                                        liftEntityQueryWrapper.eq("lift_id", liftId);
                                        LiftEntity liftEntity = liftMapper.selectOne(liftEntityQueryWrapper);
                                        Integer liftState = liftEntity.getLiftState();
                                        if (liftState == 1) {
                                            QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = setLiftAgvcTaskEntityQueryWrapper(liftId, LiftTaskStateConstant.LIFT_ASSIGNMENT_STORAGE_LOCATION);
                                            Long liftAgvTaskCount = liftAgvcTaskMapper.selectCount(liftAgvcTaskEntityQueryWrapper);
                                            if (liftAgvTaskCount == 1) {
                                                QueryWrapper<LiftAgvcTaskEntity> agvcTaskEntityQueryWrapper = setLiftAgvcTaskEntityQueryWrapper(liftId, LiftTaskStateConstant.LIFT_ASSIGNMENT_STORAGE_LOCATION);
                                                LiftAgvcTaskEntity liftAgvcTaskEntity = liftAgvcTaskMapper.selectOne(agvcTaskEntityQueryWrapper);
                                                Integer liftStartFloorTask = liftAgvcTaskEntity.getLiftStartFloor();
                                                Integer liftEndFloorTask = liftAgvcTaskEntity.getLiftEndFloor();
                                                if (Objects.equals(liftStartFloor, liftStartFloorTask) && Objects.equals(liftEndFloor, liftEndFloorTask)) {
                                                    //判断电梯是否是预约字段，预约字段需要修改储位表
                                                    if (booking == 1) {
                                                        //创建储位表
                                                        Integer i = updateLiftStorage(agvId, storageLocationId);
                                                        ifUpdateSucceed(i, "电梯储位表更新储位表成功", "电梯储位表预约字段更新失败");
                                                    }
                                                    //修改任务表状态为1,
                                                    Integer i = updateLiftTaskState(id, LiftTaskStateConstant.CALL_LIFT);
                                                    ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                                }
                                            }else {
                                                LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE);
                                            }
                                        }
                                        LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE);
                                    } else if ("0".equals(subLiftState) && subLiftId.equals(liftId)) {
                                        log.info("电梯工作获取map{}", getLiftStateLiftID);
                                        log.info("电梯状态为0");
                                        //进行呼叫梯
//                                        ChannelFuture channelFuture = CHANNEL.get(liftId);
//                                        Channel channel = channelFuture.channel();
                                        //呼梯大小次数
                                        //查询电梯协议映射表
                                        Integer selectLiftAgreement = selectLiftAgreement(LiftAgreementConstant.CALL_LIFT);
                                        //发送呼梯命令
                                        ByteBuf byteBuf2 = sendCallLift(liftStartFloor, liftEndFloor, selectLiftAgreement);
                                        String s1 = ByteBufUtil.hexDump(byteBuf2);
                                        log.info("发送呼梯命令：{}", s1);
                                        channel.writeAndFlush(byteBuf2);
                                        String string = LIFT_STATE_MAP.get(LiftTaskStateConstant.CALL_LIFT_COMMAND);
                                        if (string != null && string.equals(liftId)) {
                                            log.info("map中put的数据{}", LIFT_STATE_MAP.get(LiftTaskStateConstant.CALL_LIFT_COMMAND));
                                            if (booking == 1) {
                                                //查看是否还有预约字段，创建电梯储位
                                                QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = new QueryWrapper<>();
                                                liftAgvcTaskEntityQueryWrapper.eq("lift_id", liftId);
                                                liftAgvcTaskEntityQueryWrapper.in("booking", LiftTaskStateConstant.LIFT_STORAGE_LOCATION_RESERVED);
                                                liftAgvcTaskEntityQueryWrapper.eq("lift_start_floor", liftStartFloor);
                                                liftAgvcTaskEntityQueryWrapper.eq("lift_end_floor", liftEndFloor);
                                                List<LiftAgvcTaskEntity> liftAgvcTaskEntities = liftAgvcTaskMapper.selectList(liftAgvcTaskEntityQueryWrapper);
                                                for (LiftAgvcTaskEntity liftAgvcTaskEntity : liftAgvcTaskEntities) {
                                                    String storageLocationId1 = liftAgvcTaskEntity.getStorageLocationId();
                                                    String agvId1 = liftAgvcTaskEntity.getAgvId();
                                                    //创建储位表
                                                    Integer i = updateLiftStorage(agvId1, storageLocationId1);
                                                    ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                                }
                                                //修改电梯状态
                                                LiftEntity liftEntity = new LiftEntity();
                                                liftEntity.setLiftState(LiftConstant.LIFT_STATE_RECEIVING_TASK);
                                                QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
                                                liftEntityQueryWrapper.eq("lift_id", liftId);
                                                int update = liftMapper.update(liftEntity, liftEntityQueryWrapper);
                                                ifUpdateSucceed(update, "修改电梯表状态成功", "修改电梯表状态失败进行回滚");
                                            }
                                            //查询储位表有几个agv
                                            QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
                                            liftStorageLocationEntityQueryWrapper.eq("lift_id", liftId);
                                            liftStorageLocationEntityQueryWrapper.eq("storage_state", LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
                                            List<LiftStorageLocationEntity> liftStorageLocationEntities = liftStorageLocationMapper.selectList(liftStorageLocationEntityQueryWrapper);
                                            List<String> agvList = liftStorageLocationEntities.stream().map(LiftStorageLocationEntity::getAgvId).collect(Collectors.toList());
                                            QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = new QueryWrapper<>();
                                            liftAgvcTaskEntityQueryWrapper.eq("lift_id", liftId);
                                            liftAgvcTaskEntityQueryWrapper.in("agv_id", agvList);
                                            liftAgvcTaskEntityQueryWrapper.eq("lift_start_floor", liftStartFloor);
                                            liftAgvcTaskEntityQueryWrapper.eq("lift_end_floor", liftEndFloor);
                                            List<LiftAgvcTaskEntity> liftAgvcTaskEntities = liftAgvcTaskMapper.selectList(liftAgvcTaskEntityQueryWrapper);
                                            for (LiftAgvcTaskEntity liftAgvcTaskEntity : liftAgvcTaskEntities) {
                                                Integer id1 = liftAgvcTaskEntity.getId();
                                                //修改任务表状态为1,
                                                Integer i = updateLiftTaskState(id1, LiftTaskStateConstant.CALL_LIFT);
                                                ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                                LIFT_STATE_MAP.remove(LiftTaskStateConstant.CALL_LIFT_COMMAND);
                                                LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case 1:
                        log.info("执行状态1，当前agv：{}", agvId);
                        //查询电梯是否到达起始楼层开门到位
                        ChannelFuture channelFuture1 = CHANNEL.get(liftId);
                        if (channelFuture1 != null) {
                            Channel channel1 = channelFuture1.channel();
                            //TODO 发送一次查询指令
                            List<LiftStorageLocationEntity> liftStorageLocationEntities = liftStorageLocationMapper.selectList(null);
                            if (liftStorageLocationEntities != null && liftStorageLocationEntities.size() != 0) {
                                LiftStorageLocationEntity liftStorageLocationEntity = liftStorageLocationEntities.get(0);
                                String agvId1 = liftStorageLocationEntity.getAgvId();
                                if (agvId1.equals(agvId)) {
                                    //查询电梯协议映射表
                                    Integer integer = selectLiftAgreement(LiftAgreementConstant.SELECT_LIFT_STATE);
                                    //查询电梯当前状态
                                    ByteBuf byteBuf = selectLiftMsg(integer);
                                    log.info("发送查询电梯是否到达启始楼层，开门到位");
                                    channel1.writeAndFlush(byteBuf);
                                    String s1 = ByteBufUtil.hexDump(byteBuf);
                                    log.info("状态1发送查询电梯请求指令：{}", s1);
                                }
                            }


                            QueryWrapper<LiftEntity> liftEntityQueryWrapper = new QueryWrapper<>();
                            liftEntityQueryWrapper.eq("lift_id", liftId);
                            LiftEntity selectLift = liftMapper.selectOne(liftEntityQueryWrapper);
                            Integer liftRemainFloor = selectLift.getLiftRemainFloor();
                            Integer liftDoor = selectLift.getLiftDoor();
                            if (Objects.equals(liftRemainFloor, liftStartFloor) && Objects.equals(liftDoor, LiftConstant.LIFT_DOOR_OPEN)) {
                                ResponseVo responseVo = sendAgvEnter(dto);
                                if (responseVo.getReturnCode() == 1) {
                                    log.info("电梯已到达起始楼层，电梯门已打开");
                                    Integer i = updateLiftTaskState(id, LiftTaskStateConstant.LIFT_ARRIVE_CALL_FLOOR);
                                    ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                    LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE);
                                }
                            }
                        }
                        break;
                    case 3:
                        log.info("执行状态3，map中的参数为：{}", LIFT_SERVE_STATE_MAP.keySet());
                        //查询电梯储位大小
                        Long selectLiftStorageCount = selectLiftStorageCount(liftId);
                        //电梯储位已全部分配
                        if (selectLiftStorageCount == 2) {
                            //二辆agv都到达电梯储位执行关门
                            Long selectTaskCount = selectTaskCount(liftId, liftStartFloor, liftEndFloor, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN);
                            if (selectTaskCount == 2) {
                                //发送关门到某一层指令
                                ChannelFuture channelFuture3 = CHANNEL.get(liftId);
                                Channel channel3 = channelFuture3.channel();

                                log.info("状态3map中参数：{}", LIFT_SERVE_STATE_MAP.keySet());
                                String liftCloseFinal = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                if (liftCloseFinal == null) {
                                    String liftEndFloorString = liftEndFloor.toString();
                                    //查询电梯协议映射表
                                    Integer commandWords3 = selectLiftAgreement(LiftAgreementConstant.CLOSE_DOOR_ARRIVE_FINAL_BUILDING);
                                    log.info("查询映射表参数为：{}", commandWords3);
                                    //发送关门且到某一层指令
                                    ByteBuf msg3 = sendCloseDoorArriveFinalBuilding(commandWords3, liftEndFloorString, liftEndFloor);
                                    log.info("发送关门到某一层");
//                                ByteBuf buf3 = Unpooled.copiedBuffer(msg3, CharsetUtil.UTF_8);
                                    channel3.writeAndFlush(msg3);
                                }

                                //发送关门至某一成成功修改状态
                                int sized = LIFT_SERVE_STATE_MAP.size();
                                if (sized != 0) {
                                    String listIdString = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                    if (listIdString != null && listIdString.equals(liftId)) {
                                        //修改电梯状态为2
                                        Integer i1 = updateState(liftId);
                                        ifUpdateSucceed(i1, "修改电梯状态成功", "修改电梯状态失败进行回滚");
                                        //修改电梯里另一个agv任务状态为4
                                        QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = setLiftTask(liftId, liftStartFloor, liftEndFloor, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN);
                                        List<LiftAgvcTaskEntity> liftAgvcTaskEntities = liftAgvcTaskMapper.selectList(liftAgvcTaskEntityQueryWrapper);
                                        for (LiftAgvcTaskEntity liftAgvcTaskEntity : liftAgvcTaskEntities) {
                                            Integer TaskId = liftAgvcTaskEntity.getId();
                                            Integer i = updateLiftTaskState(TaskId, LiftTaskStateConstant.NOTIFY_AGV_LIFT_ARRIVED_START_FLOOR);
                                            ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                        }
                                        LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                    }
                                }
                            }
                        } else if (selectLiftStorageCount == 1) {
                            //等待一定时间
                            Thread.sleep(10000);
                            //查询电梯储位大小
                            Long selectedLiftStorageCount = selectLiftStorageCount(liftId);
                            log.info("电梯储位表占用大小{}", selectedLiftStorageCount);
                            if (selectedLiftStorageCount == 2) {
                                //等待agv到达点位
                                Long taskCount = selectTaskCount(liftId, liftStartFloor, liftEndFloor, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN);
                                log.info("agv到达点位大小{}", taskCount);
                                if (taskCount == 2) {
                                    //发送关门到某一层指令
                                    ChannelFuture channelFuture3 = CHANNEL.get(liftId);
                                    Channel channel3 = channelFuture3.channel();

                                    log.info("状态3map中参数：{}", LIFT_SERVE_STATE_MAP.keySet());
                                    String liftCloseFinal = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                    if (liftCloseFinal == null) {
                                        String liftEndFloorString = liftEndFloor.toString();
                                        //查询电梯协议映射表
                                        Integer commandWords3 = selectLiftAgreement(LiftAgreementConstant.CLOSE_DOOR_ARRIVE_FINAL_BUILDING);
                                        log.info("查询映射表参数为：{}", commandWords3);
                                        //发送关门且到某一层指令
                                        ByteBuf msg3 = sendCloseDoorArriveFinalBuilding(commandWords3, liftEndFloorString, liftEndFloor);
                                        log.info("发送关门到某一层");
//                                    ByteBuf buf3 = Unpooled.copiedBuffer(msg3, CharsetUtil.UTF_8);
                                        channel3.writeAndFlush(msg3);
                                    }

                                    int sized = LIFT_SERVE_STATE_MAP.size();
                                    if (sized != 0) {
                                        String liftString = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                        if (liftString != null && liftString.equals(liftId)) {
                                            //修改电梯状态为2
                                            Integer i1 = updateState(liftId);
                                            ifUpdateSucceed(i1, "修改电梯状态成功", "修改电梯状态失败进行回滚");
                                            //修改电梯里另一个agv任务状态为4
                                            QueryWrapper<LiftAgvcTaskEntity> liftAgvTaskEntityQueryWrapper = setLiftTask(liftId, liftStartFloor, liftEndFloor, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN);
                                            List<LiftAgvcTaskEntity> liftAgvcTaskEntities = liftAgvcTaskMapper.selectList(liftAgvTaskEntityQueryWrapper);
                                            for (LiftAgvcTaskEntity liftAgvcTaskEntity : liftAgvcTaskEntities) {
                                                Integer TaskId = liftAgvcTaskEntity.getId();
                                                Integer i = updateLiftTaskState(TaskId, LiftTaskStateConstant.NOTIFY_AGV_LIFT_ARRIVED_START_FLOOR);
                                                ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                            }
                                            LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                        }
                                    }
                                }
                            } else if (selectedLiftStorageCount == 1) {
                                //关门到某一层
                                ChannelFuture channelFuture3 = CHANNEL.get(liftId);
                                Channel channel3 = channelFuture3.channel();

                                log.info("状态3map中参数：{}", LIFT_SERVE_STATE_MAP.keySet());
                                String liftCloseFinal = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                if (liftCloseFinal == null) {
                                    String liftEndFloorString = liftEndFloor.toString();
                                    //查询电梯协议映射表
                                    Integer commandWords3 = selectLiftAgreement(LiftAgreementConstant.CLOSE_DOOR_ARRIVE_FINAL_BUILDING);
                                    log.info("查询映射表参数为：{}", commandWords3);
                                    //发送关门且到某一层指令
                                    ByteBuf msg3 = sendCloseDoorArriveFinalBuilding(commandWords3, liftEndFloorString, liftEndFloor);
                                    log.info("发送关门到某一层");
//                                ByteBuf buf3 = Unpooled.copiedBuffer(msg3, CharsetUtil.UTF_8);
                                    String s = ByteBufUtil.hexDump(msg3);
                                    log.info("发送关门到某一层指令为：{}", s);
                                    channel3.writeAndFlush(msg3);
                                }

                                int sized = LIFT_SERVE_STATE_MAP.size();
                                log.info("关门到某一层，大小为：{}", sized);
                                if (sized != 0) {
                                    String liftString = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                    log.info("打印map参数：{}", liftString);
                                    if (liftString != null && liftString.equals(liftId)) {
                                        //修改电梯状态为2
                                        Integer i1 = updateState(liftId);
                                        ifUpdateSucceed(i1, "修改电梯状态成功", "修改电梯状态失败进行回滚");
                                        Integer i = updateLiftTaskState(id, LiftTaskStateConstant.NOTIFY_AGV_LIFT_ARRIVED_START_FLOOR);
                                        ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                        LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.LIFT_CLOSE_FINAL_FLOOR);
                                        log.info("状态3等待一定时间后，储位大小为1时，map中的参数为：{}", LIFT_SERVE_STATE_MAP.keySet());
                                    }
                                }
                            }
                        }
                        break;
                    case 4:
                        //查询电梯是否到达最终楼层开门到位
                        ChannelFuture channelFuture4 = CHANNEL.get(liftId);
                        Channel channel4 = channelFuture4.channel();

                        log.info("状态4map中参数：{}", LIFT_SERVE_STATE_MAP.keySet());

                        //TODO 发送一次查询指令
                        List<LiftStorageLocationEntity> liftStorageLocationEntities = liftStorageLocationMapper.selectList(null);
                        if (liftStorageLocationEntities != null && liftStorageLocationEntities.size() != 0) {
                            LiftStorageLocationEntity liftStorageLocationEntity = liftStorageLocationEntities.get(0);
                            String agvId1 = liftStorageLocationEntity.getAgvId();
                            if (agvId1.equals(agvId)) {
                                //查询电梯协议映射表
                                Integer integer1 = selectLiftAgreement(LiftAgreementConstant.SELECT_LIFT_STATE);
                                //查询电梯当前状态
                                ByteBuf byteBuf2 = selectLiftMsg(integer1);
                                channel4.writeAndFlush(byteBuf2);
                                String s = ByteBufUtil.hexDump(byteBuf2);
                                log.info("发送查询电梯是否到达最终楼层，开门到位,发送查询指令：{}", s);
                            }
                        }

                        QueryWrapper<LiftEntity> wrapper = new QueryWrapper<>();
                        wrapper.eq("lift_id", liftId);
                        LiftEntity liftEntity = liftMapper.selectOne(wrapper);
                        Integer liftFinalFloor = liftEntity.getLiftRemainFloor();
                        Integer liftDoorOpen = liftEntity.getLiftDoor();
                        if (Objects.equals(liftFinalFloor, liftEndFloor) && Objects.equals(liftDoorOpen, LiftConstant.LIFT_DOOR_OPEN)) {
                            log.info("dao参数：{}", agvLiftEndFloorDto);
                            ResponseVo responseVo = sendStorage(agvLiftEndFloorDto);
                            if (responseVo.getReturnCode() == 1) {
                                log.info("电梯已到达最终楼层，电梯门已打开");
                                Integer i = updateLiftTaskState(id, LiftTaskStateConstant.AGV_ARRIVE_LIFT_STORAGE);
                                ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE);
                            }
                        }
                        break;
                    case 6:
                        log.info("执行状态6");
                        QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
                        liftStorageLocationEntityQueryWrapper.eq("lift_id", liftId);
                        liftStorageLocationEntityQueryWrapper.eq("storage_state", LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
                        Long selectCount = liftStorageLocationMapper.selectCount(liftStorageLocationEntityQueryWrapper);
                        Long taskCount = selectTaskCount(liftId, liftStartFloor, liftEndFloor, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_CLOSED);
                        log.info("储位大小{}，任务大小{}", selectCount, taskCount);
                        if (taskCount == 2 && selectCount == 0) {
                            log.info("agv大小为2");
                            //发送关门指令
                            ChannelFuture channelFuture6 = CHANNEL.get(liftId);
                            Channel channel6 = channelFuture6.channel();

                            log.info("状态6map中参数：{}", LIFT_SERVE_STATE_MAP.keySet());
                            String liftCloseDoor = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_DOOR);
                            if (liftCloseDoor == null) {
                                Integer commandWords6 = selectLiftAgreement(LiftAgreementConstant.CLOSE_DOOR);
                                //发送关门指令
                                ByteBuf msg6 = sendCloseDoor(commandWords6);
                                log.info("agv已全部离开电梯储位，发送关门指令");
//                            ByteBuf buf6 = Unpooled.copiedBuffer(msg6, CharsetUtil.UTF_8);
                                channel6.writeAndFlush(msg6);
                            }
                            int sized = LIFT_SERVE_STATE_MAP.size();
                            log.info("关门指令大小为{}，LIFT_SERVE_STATE_MAP参数为：{}", sized, LIFT_SERVE_STATE_MAP.keySet());
                            if (sized != 0) {
                                String liftIdStrign = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_DOOR);
                                log.info("执行关门命令状态为{}", liftIdStrign);
                                if (liftIdStrign != null && liftIdStrign.equals(liftId)) {
                                    QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = setLiftTask(liftId, liftStartFloor, liftEndFloor, LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_CLOSED);
                                    List<LiftAgvcTaskEntity> liftAgvcTaskEntities = liftAgvcTaskMapper.selectList(liftAgvcTaskEntityQueryWrapper);
                                    for (LiftAgvcTaskEntity liftAgvcTaskEntity : liftAgvcTaskEntities) {
                                        Integer liftTaskId = liftAgvcTaskEntity.getId();
                                        Integer i = updateLiftTaskState(liftTaskId, LiftTaskStateConstant.LIFT_ARRIVE_FINAL_FLOOR);
                                        ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                    }
                                    LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.LIFT_CLOSE_DOOR);
                                    log.info("关门结束后指令大小为{}", sized);
                                }
                            }
                        } else if (taskCount == 1 && selectCount == 0) {
                            log.info("agv大小为1");
                            //发送关门指令
                            ChannelFuture channelFuture6 = CHANNEL.get(liftId);
                            Channel channel6 = channelFuture6.channel();

                            log.info("状态6map中参数：{}", LIFT_SERVE_STATE_MAP.keySet());
                            String liftCloseDoor = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_DOOR);
                            if (liftCloseDoor == null) {

                                Integer commandWords6 = selectLiftAgreement(LiftAgreementConstant.CLOSE_DOOR);
                                //发送关门指令
                                ByteBuf msg6 = sendCloseDoor(commandWords6);
                                log.info("一辆agv已离开电梯储位");
//                            ByteBuf buf6 = Unpooled.copiedBuffer(msg6, CharsetUtil.UTF_8);
                                channel6.writeAndFlush(msg6);
                            }
                            int sized = LIFT_SERVE_STATE_MAP.size();
                            if (sized != 0) {
                                String liftIdStrign = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.LIFT_CLOSE_DOOR);
                                log.info("执行关门命令状态为{}", liftIdStrign);
                                if (liftIdStrign != null && liftIdStrign.equals(liftId)) {
                                    Integer i = updateLiftTaskState(id, LiftTaskStateConstant.LIFT_ARRIVE_FINAL_FLOOR);
                                    ifUpdateSucceed(i, "任务表跟新状态成功", "任务表跟新状态失败进行回滚");
                                    LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.LIFT_CLOSE_DOOR);
                                }
                            }
                        }
                        break;
                    case 7:
                        log.info("执行状态7");
                        ChannelFuture channelFuture7 = CHANNEL.get(liftId);
                        Channel channel7 = channelFuture7.channel();

                        log.info("状态7map中参数：{}", LIFT_SERVE_STATE_MAP.keySet());
                        //TODO 发送一次查询指令
                        //查询电梯协议映射表
                        Integer integer2 = selectLiftAgreement(LiftAgreementConstant.SELECT_LIFT_STATE);
                        //查询电梯当前状态
                        ByteBuf byteBuf3 = selectLiftMsg(integer2);
                        channel7.writeAndFlush(byteBuf3);
                        String s = ByteBufUtil.hexDump(byteBuf3);
                        log.info("发送查询指令，查看电梯门状态，发送查询指令：{}", s);


//                                Integer i = LIFT_SERVE_STATE_MAP.get(LiftTaskStateConstant.SELECT_LIFT_DOOR_STATE);
                        QueryWrapper<LiftEntity> entityQueryWrapper = new QueryWrapper<>();
                        entityQueryWrapper.eq("lift_id", liftId);
                        LiftEntity lift = liftMapper.selectOne(entityQueryWrapper);
                        Integer door = lift.getLiftDoor();
                        log.info("查询电梯门状态{}", door);
                        if (door != null && door == 0) {
                            log.info("电梯关门到位，释放电梯");
                            QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = setLiftTask(liftId, liftStartFloor, liftEndFloor, LiftTaskStateConstant.LIFT_ARRIVE_FINAL_FLOOR);
                            List<LiftAgvcTaskEntity> liftAgvcTaskEntities = liftAgvcTaskMapper.selectList(liftAgvcTaskEntityQueryWrapper);
                            for (LiftAgvcTaskEntity liftAgvcTaskEntity : liftAgvcTaskEntities) {
                                Integer liftTaskId = liftAgvcTaskEntity.getId();

                                LiftEntity liftEntity1 = new LiftEntity();
                                liftEntity1.setLiftState(LiftConstant.LIFT_STATE_IDLE);
                                QueryWrapper<LiftEntity> liftEntityQueryWrapper1 = new QueryWrapper<>();
                                liftEntityQueryWrapper1.eq("lift_id", liftId);
                                int update = liftMapper.update(liftEntity1, liftEntityQueryWrapper1);
                                ifUpdateSucceed(update, "修改电梯表状态成功", "修改电梯表状态失败进行回滚");

                                //任务表移到历史任务表
                                LiftAgvcHistoryEntity liftAgvcHistoryEntity = new LiftAgvcHistoryEntity();
                                BeanUtils.copyProperties(liftAgvcTaskEntity, liftAgvcHistoryEntity);
                                liftAgvcHistoryEntity.setEndTime(new Date());
                                liftAgvcHistoryEntity.setState(LiftTaskStateConstant.LIFT_DOOR_COMPLETELY_OPEN_NOTIFY_AGV);
                                liftAgvcHistoryMapper.insert(liftAgvcHistoryEntity);
                                //删除任务表
                                liftAgvcTaskMapper.deleteById(liftTaskId);
                                LIFT_SERVE_STATE_MAP.remove(LiftTaskStateConstant.SELECT_LIFT_SERVE_STATE);
                                log.info("状态7，map中的参数为：{}", LIFT_SERVE_STATE_MAP.keySet());
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private static void ifUpdateSucceed(Integer i, String succeed, String fail) {
        if (i > 0) {
            log.info(succeed);
        } else {
            throw new RuntimeException(fail);
        }
    }

    private static ByteBuf sendCloseDoor(Integer commandWords) {
        int intValue = commandWords.intValue();
        byte[] b = new byte[7];
        b[0] = (byte) 0x09;
        b[1] = (byte) 0x00;
        b[2] = (byte) 0x00;
        b[3] = (byte) intValue;
        b[4] = (byte) 0x00;
        b[5] = (byte) 0x00;
        b[6] = (byte) 0x01;
        byte checksum = calculateChecksum(b);
//        Integer integer = Integer.valueOf(checksum);
//        String msg = "aa090000" + commandWords + "000001" + checksum + "bb";
        log.info("发送关门命令，命令字：{}", intValue);

        ByteBuf buffer = Unpooled.buffer(10);
        buffer.writeByte(0xaa);
        buffer.writeByte(0x09);
        buffer.writeByte(0x00);
        buffer.writeByte(0x00);
        buffer.writeByte(commandWords);
        buffer.writeByte(0x00);
        buffer.writeByte(0x00);
        buffer.writeByte(0x01);
        buffer.writeByte(checksum);
        buffer.writeByte(0xbb);
        return buffer;
    }

    private static ByteBuf sendCloseDoorArriveFinalBuilding(Integer commandWords, String liftEndFloorString, Integer liftEndFloor) {
        int intValue = commandWords.intValue();
        byte b1 = Byte.parseByte(liftEndFloorString, 16);
        byte[] b = new byte[7];
        b[0] = (byte) 0x09;
        b[1] = (byte) 0x00;
        b[2] = (byte) 0x00;
        b[3] = (byte) intValue;
        b[4] = (byte) b1;
        b[5] = (byte) 0x00;
        b[6] = (byte) 0x01;
        byte checksum = calculateChecksum(b);
//        Integer integer = Integer.valueOf(checksum);
        log.info("发送关门且到某一层命令，命令字：{}", intValue);

        ByteBuf buffer = Unpooled.buffer(10);
        buffer.writeByte(0xaa);
        buffer.writeByte(0x09);
        buffer.writeByte(0x00);
        buffer.writeByte(0x00);
        buffer.writeByte(commandWords);
        buffer.writeByte(liftEndFloor);
        buffer.writeByte(0x00);
        buffer.writeByte(0x01);
        buffer.writeByte(checksum);
        buffer.writeByte(0xbb);
        return buffer;
    }

    private static ByteBuf sendCallLift(Integer liftStartFloor, Integer liftEndFloor, Integer commandWords) {
        String liftStart = liftStartFloor.toString();
        byte b1 = Byte.parseByte(liftStart, 16);
        String liftEnd = liftEndFloor.toString();
        byte b2 = Byte.parseByte(liftEnd, 16);
        byte byteValue = commandWords.byteValue();
        byte[] b = new byte[7];
        b[0] = (byte) 0x09;
        b[1] = (byte) 0x00;
        b[2] = (byte) 0x00;
        b[3] = byteValue;
        log.info("查询数据库呼梯参数：{}", byteValue);
        b[4] = b1;
        log.info("查询数据库呼梯任务启始楼层：{}", b1);
        b[5] = b2;
        log.info("查询数据库呼梯任务最终楼层：{}", b2);
        b[6] = (byte) 0x01;
        byte checksum = calculateChecksum(b);
        log.info("呼梯校验和：{}", checksum);
//        Integer integer = Integer.valueOf(checksum);

        ByteBuf buffer = Unpooled.buffer(10);
        buffer.writeByte(0xaa);
        buffer.writeByte(0x09);
        buffer.writeByte(0x00);
        buffer.writeByte(0x00);
        buffer.writeByte(byteValue);
        buffer.writeByte(b1);
        buffer.writeByte(b2);
        buffer.writeByte(0x01);
        buffer.writeByte(checksum);
        buffer.writeByte(0xbb);
        return buffer;
    }

    private Integer selectLiftAgreement(String liftCommandId) {
        QueryWrapper<liftAgreementEntity> liftAgreementEntityQueryWrapper = new QueryWrapper<>();
        liftAgreementEntityQueryWrapper.eq("lift_command_id", liftCommandId);
        liftAgreementEntity liftAgreementEntity = liftAgreementMapper.selectOne(liftAgreementEntityQueryWrapper);
        Integer commandWords = liftAgreementEntity.getCommandWords();
        return commandWords;
    }

    private Long selectLiftStorageCount(String liftId) {
        QueryWrapper<LiftStorageLocationEntity> locationEntityQueryWrapper = new QueryWrapper<>();
        locationEntityQueryWrapper.eq("lift_id", liftId);
        locationEntityQueryWrapper.eq("storage_state", LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
        Long selectLiftStorageCount = liftStorageLocationMapper.selectCount(locationEntityQueryWrapper);
        return selectLiftStorageCount;
    }

    private static ByteBuf selectLiftMsg(Integer commandWords) {
        byte byteValue = commandWords.byteValue();
        log.info("装换为byte类型参数为：{}", byteValue);
        byte[] b = new byte[7];
        b[0] = (byte) 0x09;
        b[1] = (byte) 0x00;
        b[2] = (byte) 0x00;
        b[3] = byteValue;
//        b[3] = (byte) 0x40;
        b[4] = (byte) 0x00;
        b[5] = (byte) 0x00;
        b[6] = (byte) 0x00;
        byte b1 = calculateChecksum(b);
//        String msg = "aa090001" + commandWords + "000000" + checksum + "bb";
        ByteBuf buffer = Unpooled.buffer(10);
        buffer.writeByte(0xaa);
        buffer.writeByte(0x09);
        buffer.writeByte(0x00);
        buffer.writeByte(0x00);
        buffer.writeByte(commandWords);
        log.info("查询电梯当前状态命令字,发送参数为：{}", commandWords);
        buffer.writeByte(0x00);
        buffer.writeByte(0x00);
        buffer.writeByte(0x00);
        buffer.writeByte(b1);
        buffer.writeByte(0xbb);

        return buffer;
    }

    private Integer updateState(String liftId) {
        LiftEntity lift = new LiftEntity();
        lift.setLiftState(LiftConstant.LIFT_STATE_EXECUTING_TASK);
        QueryWrapper<LiftEntity> entityQueryWrapper = new QueryWrapper<>();
        entityQueryWrapper.eq("lift_id", liftId);
        int update = liftMapper.update(lift, entityQueryWrapper);
        return update;
    }

    private static QueryWrapper<LiftAgvcTaskEntity> setLiftTask(String liftId, Integer liftStartFloor, Integer liftEndFloor, Integer state) {
        QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = new QueryWrapper<>();
        liftAgvcTaskEntityQueryWrapper.eq("lift_id", liftId);
        liftAgvcTaskEntityQueryWrapper.eq("lift_start_floor", liftStartFloor);
        liftAgvcTaskEntityQueryWrapper.eq("lift_end_floor", liftEndFloor);
        liftAgvcTaskEntityQueryWrapper.eq("state", state);
        return liftAgvcTaskEntityQueryWrapper;
    }

    private Long selectTaskCount(String liftId, Integer liftStartFloor, Integer liftEndFloor, Integer state) {
        QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = new QueryWrapper<>();
        liftAgvcTaskEntityQueryWrapper.eq("lift_id", liftId);
        liftAgvcTaskEntityQueryWrapper.eq("lift_start_floor", liftStartFloor);
        liftAgvcTaskEntityQueryWrapper.eq("lift_end_floor", liftEndFloor);
        liftAgvcTaskEntityQueryWrapper.eq("state", state);
        Long selectTaskCount = liftAgvcTaskMapper.selectCount(liftAgvcTaskEntityQueryWrapper);
        return selectTaskCount;
    }

    private Integer updateLiftStorage(String agvId, String storageLocationId) {
        LiftStorageLocationEntity liftStorageLocationEntity = new LiftStorageLocationEntity();
        liftStorageLocationEntity.setStorageState(LiftStorageLocationConstant.LIFT_STORAGE_LOCATION_UNAVAILABLE);
        liftStorageLocationEntity.setAgvId(agvId);
        QueryWrapper<LiftStorageLocationEntity> liftStorageLocationEntityQueryWrapper = new QueryWrapper<>();
        liftStorageLocationEntityQueryWrapper.eq("storage_location_id", storageLocationId);
        int update = liftStorageLocationMapper.update(liftStorageLocationEntity, liftStorageLocationEntityQueryWrapper);
        return update;
    }

    private static QueryWrapper<LiftAgvcTaskEntity> setLiftAgvcTaskEntityQueryWrapper(String liftId, Integer state) {
        QueryWrapper<LiftAgvcTaskEntity> liftAgvcTaskEntityQueryWrapper = new QueryWrapper<>();
        liftAgvcTaskEntityQueryWrapper.eq("lift_id", liftId);
        liftAgvcTaskEntityQueryWrapper.ne("state", state);
        return liftAgvcTaskEntityQueryWrapper;
    }

    private Integer updateLiftTaskState(Integer id, Integer state) {
        LiftAgvcTaskEntity liftAgvTaskEntity = new LiftAgvcTaskEntity();
        liftAgvTaskEntity.setState(state);
        QueryWrapper<LiftAgvcTaskEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int update = liftAgvcTaskMapper.update(liftAgvTaskEntity, queryWrapper);
        return update;
    }

    public static byte sumCheck(byte[] b) {
        int sum = 0;
        for (byte value : b) {
            sum += value;
        }
        if (sum > 0xFF) { // 超过了255，使用补码（补码 = 原码取反 + 1）
            sum = ~sum;
            sum += 1;
        }
        return (byte) (sum & 0xFF);
    }

    public static byte byteToHex(byte b) {
        byte hex = (byte) (b & 0xFF);
        return hex;
    }

    public static byte calculateChecksum(byte[] data) {
        byte result = sumCheck(data);
        return byteToHex(result);
    }

    //通知agv进入电梯
    public ResponseVo sendAgvEnter(AgvLiftStorageLocationDto dto) {
        String WCS_URL = "http://".concat("10.1.12.182").concat(":").concat("7777").concat("/rpc/lift/enter");
//        String WCS_URL = "http://".concat("localhost").concat(":").concat("8081").concat("/lift/enter");
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将wcsRequest转为json格式
            String jsonBody = objectMapper.writeValueAsString(dto);
            log.info("将dto转json格式：{}", jsonBody);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
            log.info("请求头：{}", requestEntity);
            // 将响应体转为wcsResponse
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(WCS_URL, requestEntity, String.class);
            String responseBody = responseEntity.getBody();
            return objectMapper.readValue(responseBody, ResponseVo.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    //电梯到达最终楼层，通知agv离开电梯
    public ResponseVo sendStorage(AgvLiftEndFloorDto dto) {
        String WCS_URL = "http://".concat("10.1.12.182").concat(":").concat("7777").concat("/rpc/lift/arrive");
//        String WCS_URL = "http://".concat("localhost").concat(":").concat("8081").concat("/lift/arrive");
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 将wcsRequest转为json格式
            String jsonBody = objectMapper.writeValueAsString(dto);
            log.info("将dto转json格式：{}", jsonBody);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
            log.info("请求头：{}", requestEntity);
            // 将响应体转为wcsResponse
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(WCS_URL, requestEntity, String.class);
            String responseBody = responseEntity.getBody();
            return objectMapper.readValue(responseBody, ResponseVo.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}